﻿CREATE function PROPERCASE(@s varchar(255)) 
--Returns a string with the first letter of each word at the beginning of a sentence  in uppercase, all other letters in lowercase
returns  varchar(255)
as
BEGIN
DECLARE @i int, @c char(1),@result varchar(255)
SET @result=LOWER(@s)
SET @i=2
SET @result=STUFF(@result,1,1,UPPER(SUBSTRING(@s,1,1)))
WHILE @i<=LEN(@s)
	BEGIN
	SET @c=SUBSTRING(@s,@i,1)
	IF (@c='!') OR (@c='?') OR (@c='_')OR (@c='.')
		IF @i<LEN(@s)
			BEGIN
lblSeek:
			SET @i=@i+1
			IF UPPER(SUBSTRING(@s,@i,1)) LIKE '[A-Z]'
				SET @result=STUFF(@result,@i,1,UPPER(SUBSTRING(@s,@i,1)))
			ELSE
				IF @i<LEN(@s)
					GOTO lblSeek
			END
	SET @i=@i+1
	END
RETURN  @result
END


GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

CREATE function INITCAP (@s varchar(255) ) 
--Returns a string with the first letter of each word in uppercase, all other letters in lowercase (capitalize first character).  
returns varchar(255)
as
BEGIN
DECLARE @i int, @c char(1),@result varchar(255)
SET @result=LOWER(@s)
SET @i=2
SET @result=STUFF(@result,1,1,UPPER(SUBSTRING(@s,1,1)))
WHILE @i<=LEN(@s)
	BEGIN
	SET @c=SUBSTRING(@s,@i,1)
	IF (@c=' ') OR (@c=';') OR (@c=':') OR (@c='!') OR (@c='?') OR (@c=',')OR (@c='.')OR (@c='_')
		IF @i<LEN(@s)
			BEGIN
			SET @i=@i+1
			SET @result=STUFF(@result,@i,1,UPPER(SUBSTRING(@s,@i,1)))
			END
	SET @i=@i+1
	END
RETURN  ISNULL(@result,@s)
END




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

